package net;

import java.io.*;
import java.net.Socket;
import java.util.Scanner;

public class TCPClient {

    //服务器IP / 域名
    private static final String SERVER_HOST = "localhost";
    //服务器Socket进程的端口号 -> 所以她是和服务器那边的端口是保持一致的
    private static final int SERVER_PORT = 4090;

    private String host = null;
    private int port = 0;
    private Socket socket = null;
    public TCPClient(String host, int port) throws IOException{
        socket = new Socket(host, port);
    }

    public static void main(String[] args) throws IOException{
        TCPClient tcpClient = new TCPClient(SERVER_HOST, SERVER_PORT);
        tcpClient.start(tcpClient.socket);
    }

    private void start (Socket socket) throws IOException {
        System.out.println("[" + socket.getPort() +"/" +
                        socket.getInetAddress() + "]" + "客户端上线！");
        Scanner scanner = new Scanner(System.in);

        try (InputStream is = socket.getInputStream();
             OutputStream os = socket.getOutputStream()) {
            while (true) {
//                System.out.print("->");
//                String request = scanner.next();
//                byte[] requestData = request.getBytes();
//                os.write(requestData);
//                os.flush();

                //使用pw可以直接将Scanner的输入进行输出，而直接使用is则需要转为字节流
                PrintWriter pw = new PrintWriter(socket.getOutputStream());
                System.out.print("->");
                pw.println(scanner.next());
                pw.flush();

                Scanner scanner1 = new Scanner(is);
                System.out.println(scanner1.next());
            }

        }
    }

    public static void main1(String[] args) throws IOException {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("-------------");
            System.out.print("请输入要展示的目录：");
            String request = scanner.next();

            Socket client = new Socket(SERVER_HOST, SERVER_PORT);
            OutputStream os = client.getOutputStream();
            PrintWriter pw = new PrintWriter(new OutputStreamWriter(os, "UTF-8"));

            pw.println(request);
            pw.flush();

            System.out.println("接到服务器响应：");
            InputStream is = client.getInputStream();
            BufferedReader bf = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            String line = new String();

            while ((line = bf.readLine()) != null) {
                System.out.println(line);
            }
            client.close();
        }
    }

    public static void main0(String[] args) throws IOException {

        //创建客户端套接字Socket，对应到IP主机，与对应端口的进程进行连接
        Socket client = new Socket(SERVER_HOST, SERVER_PORT);
        //发送TCP数据，通过socket中的输出流进行发送
        OutputStream os = client.getOutputStream();
        //将以上字节流包装成字符流
        PrintWriter pw = new PrintWriter(new OutputStreamWriter(os, "UTF-8"));

        //发送数据？？？
        pw.println("黎雨琳！");
        //有缓冲区的IO操作，真正传输数据需要刷新缓冲区
        pw.flush();

        client.close();

    }

}
